package ua.edu.nuos.jeetraining2012.cms.entity;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;

/**
 * Normalization class for Students and HomeWorks.
 *
 * @author Victor N. Ganichev victor.ganichev@gmail.com
 * @version 1.0 Date: 23.05.13 Time: 23:49
 * @since 1.0
 */
@Entity
@Table(name = "students_to_homework")
@IdClass(StudentsToHomeWorkId.class)
public class StudentsToHomeWorkEntity implements Serializable {

    /**
     * Student id, FK to Students.
     */
    @Id
    @ManyToOne(optional = false)
    @JoinColumn(name = "student_id")
    @NotNull
    private UserEntity studentId;

    /**
     * Homework id, FK to homework
     */
    @Id
    @ManyToOne(optional = false)
    @JoinColumn(name = "homework_id")
    @NotNull
    private HomeWorkEntity homeworkId;

    /**
     * Date of passing attempt.
     */
    @Column(name = "attempt_date", nullable = false)
    @Temporal(value = TemporalType.TIMESTAMP)
    @Basic(optional = false)
    @NotNull
    private Date attemptDate;

    /**
     * Scores, that student gets for this HW.
     */
    @Column(name = "scores", nullable = false)
    @Basic(optional = false)
    @NotNull
    private int sroces;

    /**
     * Flag, that indicates, that HW done. This flag is switching by instructor.
     */
    @Column(name = "is_done", columnDefinition = "BOOLEAN DEFAULT false")
    @Basic(optional = false)
    @NotNull
    private boolean isDone;

    // Default constructor.
    public StudentsToHomeWorkEntity() {}

    // Getters & Setters
    public UserEntity getStudentId() {
        return studentId;
    }

    public void setStudentId(UserEntity studentId) {
        this.studentId = studentId;
    }

    public HomeWorkEntity getHomeworkId() {
        return homeworkId;
    }

    public void setHomeworkId(HomeWorkEntity homeworkId) {
        this.homeworkId = homeworkId;
    }

    public Date getAttemptDate() {
        return attemptDate;
    }

    public void setAttemptDate(Date attemptDate) {
        this.attemptDate = attemptDate;
    }

    public int getSroces() {
        return sroces;
    }

    public void setSroces(int sroces) {
        this.sroces = sroces;
    }

    public boolean isDone() {
        return isDone;
    }

    public void setDone(boolean done) {
        isDone = done;
    }

    // Overrides
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        StudentsToHomeWorkEntity that = (StudentsToHomeWorkEntity) o;

        if (homeworkId != null ? !homeworkId.equals(that.homeworkId) : that.homeworkId != null) return false;
        if (studentId != null ? !studentId.equals(that.studentId) : that.studentId != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = studentId != null ? studentId.hashCode() : 0;
        result = 31 * result + (homeworkId != null ? homeworkId.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "StudentsToHomeWorkEntity{" +
                "studentId=" + studentId.getId() +
                ", homeworkId=" + homeworkId.getId() +
                ", attemptDate=" + attemptDate +
                ", sroces=" + sroces +
                ", isDone=" + isDone +
                '}';
    }
}
